package com.mengxiangnongfu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mengxiangnongfu.dto.OperLogDto;
import com.mengxiangnongfu.vo.DataGridView;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mengxiangnongfu.domain.OperLog;
import com.mengxiangnongfu.mapper.OperLogMapper;
import com.mengxiangnongfu.service.OperLogService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class OperLogServiceImpl extends ServiceImpl<OperLogMapper, OperLog> implements OperLogService {

    @Resource
    private OperLogMapper operLogMapper;

    @Override
    public int insertOperLog(OperLog operLog) {
        return operLogMapper.insert(operLog);
    }

    @Override
    public DataGridView loadOperLogByPage(OperLogDto operLogDto) {
        Page<OperLog> page = new Page<>(operLogDto.getPageNum(), operLogDto.getPageSize());
        QueryWrapper<OperLog> queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(operLogDto.getTitle()), OperLog.COL_TITLE, operLogDto.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(operLogDto.getOperName()), OperLog.COL_OPER_NAME, operLogDto.getOperName());
        queryWrapper.eq(StringUtils.isNotBlank(operLogDto.getBusinessType()), OperLog.COL_BUSINESS_TYPE, operLogDto.getBusinessType());
        queryWrapper.eq(StringUtils.isNotBlank(operLogDto.getStatus()), OperLog.COL_STATUS, operLogDto.getStatus());
        queryWrapper.ge(null != operLogDto.getBeginTime(), OperLog.COL_OPER_TIME, operLogDto.getBeginTime());
        queryWrapper.le(null != operLogDto.getBeginTime(), OperLog.COL_OPER_TIME, operLogDto.getEndTime());
        queryWrapper.orderByDesc(OperLog.COL_OPER_TIME);
        this.operLogMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public int removeOperLogByIds(Long[] ids) {
        if (null != ids && ids.length > 0) {
            return this.operLogMapper.deleteBatchIds(Arrays.asList(ids));
        } else {
            return 0;
        }
    }

    @Override
    public int clearAllOperLogs() {
        return this.operLogMapper.delete(null);
    }
}
